<html>
<head><meta charset="utf-8"><title>Documentation that Box doesn&#x27;t move the value in the heap? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Documentation.20that.20Box.20doesn&#x27;t.20move.20the.20value.20in.20the.20heap.3F.html">Documentation that Box doesn&#x27;t move the value in the heap?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="216876512"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Documentation%20that%20Box%20doesn%27t%20move%20the%20value%20in%20the%20heap%3F/near/216876512" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Documentation.20that.20Box.20doesn&#x27;t.20move.20the.20value.20in.20the.20heap.3F.html#216876512">(Nov 16 2020 at 14:19)</a>:</h4>
<p>Is it documented anywhere that <code>let a = Box::new(42); let b = a</code> is guaranteed to not change the pointer to <code>42</code>? I know we rely on this in places, but it's not listed in <code>std::box</code> or <code>std::box::Box</code>.</p>



<a name="216877183"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Documentation%20that%20Box%20doesn%27t%20move%20the%20value%20in%20the%20heap%3F/near/216877183" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Documentation.20that.20Box.20doesn&#x27;t.20move.20the.20value.20in.20the.20heap.3F.html#216877183">(Nov 16 2020 at 14:24)</a>:</h4>
<p>How could it change the pointer? Moves are always bitwise copies if they copy at all</p>



<a name="216877457"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Documentation%20that%20Box%20doesn%27t%20move%20the%20value%20in%20the%20heap%3F/near/216877457" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Documentation.20that.20Box.20doesn&#x27;t.20move.20the.20value.20in.20the.20heap.3F.html#216877457">(Nov 16 2020 at 14:26)</a>:</h4>
<p>(and we guarantee it is indeed a pointer, and there's no small-box magic going on; "a Box&lt;T&gt; is guaranteed to be represented as a single pointer")</p>



<a name="216879105"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Documentation%20that%20Box%20doesn%27t%20move%20the%20value%20in%20the%20heap%3F/near/216879105" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Documentation.20that.20Box.20doesn&#x27;t.20move.20the.20value.20in.20the.20heap.3F.html#216879105">(Nov 16 2020 at 14:38)</a>:</h4>
<p><span class="user-mention silent" data-user-id="232545">Joshua Nelson</span> <a href="#narrow/stream/122651-general/topic/Documentation.20that.20Box.20doesn't.20move.20the.20value.20in.20the.20heap.3F/near/216877183">said</a>:</p>
<blockquote>
<p>How could it change the pointer? Moves are always bitwise copies if they copy at all</p>
</blockquote>
<p>I'm not saying it <em>could</em>, I'm just pointing out that it's not explicitly stated :-)</p>



<a name="216879153"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Documentation%20that%20Box%20doesn%27t%20move%20the%20value%20in%20the%20heap%3F/near/216879153" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Documentation.20that.20Box.20doesn&#x27;t.20move.20the.20value.20in.20the.20heap.3F.html#216879153">(Nov 16 2020 at 14:38)</a>:</h4>
<p>"How could" can always be answered by "compiler magic" after all</p>



<a name="216879236"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Documentation%20that%20Box%20doesn%27t%20move%20the%20value%20in%20the%20heap%3F/near/216879236" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Documentation.20that.20Box.20doesn&#x27;t.20move.20the.20value.20in.20the.20heap.3F.html#216879236">(Nov 16 2020 at 14:39)</a>:</h4>
<p>well ok, but I think 'compiler magic' only applies when we say it is happening <em>somehow</em> and we're evasive on how</p>



<a name="216879251"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Documentation%20that%20Box%20doesn%27t%20move%20the%20value%20in%20the%20heap%3F/near/216879251" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Documentation.20that.20Box.20doesn&#x27;t.20move.20the.20value.20in.20the.20heap.3F.html#216879251">(Nov 16 2020 at 14:39)</a>:</h4>
<p>'compiler magic' shouldn't make arbitrary changes to semantics</p>



<a name="216879259"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Documentation%20that%20Box%20doesn%27t%20move%20the%20value%20in%20the%20heap%3F/near/216879259" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Documentation.20that.20Box.20doesn&#x27;t.20move.20the.20value.20in.20the.20heap.3F.html#216879259">(Nov 16 2020 at 14:39)</a>:</h4>
<p>otherwise you couldn't trust the language</p>



<a name="216879407"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Documentation%20that%20Box%20doesn%27t%20move%20the%20value%20in%20the%20heap%3F/near/216879407" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Documentation.20that.20Box.20doesn&#x27;t.20move.20the.20value.20in.20the.20heap.3F.html#216879407">(Nov 16 2020 at 14:40)</a>:</h4>
<p>I would not be opposed to a brief note, but I think it basically comes down to "moves are defined to not change the value of a struct"</p>



<a name="216879682"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Documentation%20that%20Box%20doesn%27t%20move%20the%20value%20in%20the%20heap%3F/near/216879682" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Documentation.20that.20Box.20doesn&#x27;t.20move.20the.20value.20in.20the.20heap.3F.html#216879682">(Nov 16 2020 at 14:43)</a>:</h4>
<p>Another option to be explicit would be to move the StableDeref trait into std: &lt;<a href="https://docs.rs/stable_deref_trait/">https://docs.rs/stable_deref_trait/</a>&gt;</p>



<a name="216880627"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Documentation%20that%20Box%20doesn%27t%20move%20the%20value%20in%20the%20heap%3F/near/216880627" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Documentation.20that.20Box.20doesn&#x27;t.20move.20the.20value.20in.20the.20heap.3F.html#216880627">(Nov 16 2020 at 14:51)</a>:</h4>
<p>I don't see what that has to do with this :/ where is deref involved?</p>



<a name="216880718"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Documentation%20that%20Box%20doesn%27t%20move%20the%20value%20in%20the%20heap%3F/near/216880718" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Documentation.20that.20Box.20doesn&#x27;t.20move.20the.20value.20in.20the.20heap.3F.html#216880718">(Nov 16 2020 at 14:51)</a>:</h4>
<p>oh I see, it's makign a guarentee around Deref</p>



<a name="216911454"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Documentation%20that%20Box%20doesn%27t%20move%20the%20value%20in%20the%20heap%3F/near/216911454" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Documentation.20that.20Box.20doesn&#x27;t.20move.20the.20value.20in.20the.20heap.3F.html#216911454">(Nov 16 2020 at 18:25)</a>:</h4>
<p>does <code>Pin</code>/etc. currently force this guarantee?</p>



<a name="216911640"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Documentation%20that%20Box%20doesn%27t%20move%20the%20value%20in%20the%20heap%3F/near/216911640" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Documentation.20that.20Box.20doesn&#x27;t.20move.20the.20value.20in.20the.20heap.3F.html#216911640">(Nov 16 2020 at 18:27)</a>:</h4>
<p>maybe <code>Unpin for Box&lt;T&gt;</code>?</p>



<a name="216911955"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Documentation%20that%20Box%20doesn%27t%20move%20the%20value%20in%20the%20heap%3F/near/216911955" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Documentation.20that.20Box.20doesn&#x27;t.20move.20the.20value.20in.20the.20heap.3F.html#216911955">(Nov 16 2020 at 18:29)</a>:</h4>
<p>that's not the guarantee that unpin provides, and Box is already unpin</p>



<a name="216912174"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Documentation%20that%20Box%20doesn%27t%20move%20the%20value%20in%20the%20heap%3F/near/216912174" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Documentation.20that.20Box.20doesn&#x27;t.20move.20the.20value.20in.20the.20heap.3F.html#216912174">(Nov 16 2020 at 18:30)</a>:</h4>
<p>I know it's already there, but pinning still confuses me, so I wasn't sure what that actually promises</p>



<a name="216912193"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Documentation%20that%20Box%20doesn%27t%20move%20the%20value%20in%20the%20heap%3F/near/216912193" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Documentation.20that.20Box.20doesn&#x27;t.20move.20the.20value.20in.20the.20heap.3F.html#216912193">(Nov 16 2020 at 18:31)</a>:</h4>
<p>(no need to go on that tangent here if it's not relevant)</p>



<a name="216936803"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Documentation%20that%20Box%20doesn%27t%20move%20the%20value%20in%20the%20heap%3F/near/216936803" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Documentation.20that.20Box.20doesn&#x27;t.20move.20the.20value.20in.20the.20heap.3F.html#216936803">(Nov 16 2020 at 21:44)</a>:</h4>
<p><span class="user-mention silent" data-user-id="232545">Joshua Nelson</span> <a href="#narrow/stream/122651-general/topic/Documentation.20that.20Box.20doesn't.20move.20the.20value.20in.20the.20heap.3F/near/216877183">said</a>:</p>
<blockquote>
<p>Moves are always bitwise copies if they copy at all</p>
</blockquote>
<p><span class="user-mention silent" data-user-id="116122">simulacrum</span> <a href="#narrow/stream/122651-general/topic/Documentation.20that.20Box.20doesn't.20move.20the.20value.20in.20the.20heap.3F/near/216879407">said</a>:</p>
<blockquote>
<p>moves are defined to not change the value of a struct</p>
</blockquote>
<p>What's the best place to point to for that guarantee then? <span aria-label="innocent" class="emoji emoji-1f607" role="img" title="innocent">:innocent:</span></p>



<a name="216937099"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Documentation%20that%20Box%20doesn%27t%20move%20the%20value%20in%20the%20heap%3F/near/216937099" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Documentation.20that.20Box.20doesn&#x27;t.20move.20the.20value.20in.20the.20heap.3F.html#216937099">(Nov 16 2020 at 21:47)</a>:</h4>
<p>unclear. Maybe <a href="https://doc.rust-lang.org/stable/reference/expressions.html?highlight=move#moved-and-copied-types">https://doc.rust-lang.org/stable/reference/expressions.html?highlight=move#moved-and-copied-types</a>? But that doesn't really head on address it.</p>



<a name="217121594"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Documentation%20that%20Box%20doesn%27t%20move%20the%20value%20in%20the%20heap%3F/near/217121594" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Documentation.20that.20Box.20doesn&#x27;t.20move.20the.20value.20in.20the.20heap.3F.html#217121594">(Nov 18 2020 at 11:15)</a>:</h4>
<p><span class="user-mention silent" data-user-id="243558">Steven Fackler</span> <a href="#narrow/stream/122651-general/topic/Documentation.20that.20Box.20doesn't.20move.20the.20value.20in.20the.20heap.3F/near/216911955">said</a>:</p>
<blockquote>
<p>that's not the guarantee that unpin provides, and Box is already unpin</p>
</blockquote>
<p>To be fair, the fact that <code>for&lt;Pointee : !Unpin&gt; Box&lt;Pointee&gt; : Unpin + DerefMut&lt;Target = Pointee&gt;</code> does imply that moving / <code>swap</code> / <code>replace</code>-ing a <code>Box&lt;impl !Unpin&gt;</code> does not move its pointee.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>